package com.example.recite_words.util;


import android.content.Context;
import android.os.Build;
import android.os.Looper;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.recite_words.MainActivity;
import com.example.recite_words.activity.Login;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Send_data {
    public static JSONObject do_post(Context context, String data, String Url) {
        try {

            HttpURLConnection connection = (HttpURLConnection) new URL(Url).openConnection();
            connection.setRequestMethod("POST");
            connection.setReadTimeout(5000);
            connection.setConnectTimeout(5000);
            connection.setDoOutput(true);//向服务器写数据
            connection.setDoInput(true);//接受服务器请求
            connection.setUseCaches(false);//

            //不需要请求头
             OutputStream out = connection.getOutputStream();
            out.write(data.getBytes());
            out.flush();
            int code = connection.getResponseCode();
            if (code == 200) {
                InputStream in = connection.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(in);
                JSONObject jsonObject = Get_data.getJsonObject(in);
                Looper.prepare();
                Toast.makeText(context, (String)jsonObject.get("message"), Toast.LENGTH_SHORT).show();
                //Looper.loop();
                connection.disconnect();
                return jsonObject;
            } else {
/*                InputStream in = connection.getErrorStream();
                InputStreamReader inputStreamReader = new InputStreamReader(in);
                JSONObject jsonObject = Get_data.getJsonObject(in);
                Looper.prepare();
                Toast.makeText(context, (String)jsonObject.get("message"), Toast.LENGTH_SHORT).show();*/
                connection.disconnect();
                return null;
            }

        } catch (ProtocolException e) {//数据传输协议不匹配而导致无法与远程方进行通信
            e.printStackTrace();
            return new JSONObject(Config.link_error_map);
        } catch (MalformedURLException e) {
            e.printStackTrace();//URL错误
            return new JSONObject(Config.link_error_map);
        } catch (IOException e) {
            e.printStackTrace();
            return new JSONObject(Config.io_error_map);
        }

    }

    public static String post_for_pic(String data, String Url,String path,String name) {
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(Url).openConnection();
            connection.setRequestMethod("POST");
            connection.setReadTimeout(5000);
            connection.setConnectTimeout(5000);
            connection.setDoOutput(true);//向服务器写数据
            connection.setDoInput(true);//接受服务器请求
            connection.setUseCaches(false);//

            //不需要请求头
            OutputStream out = connection.getOutputStream();
            out.write(data.getBytes());
            out.flush();
            InputStream in =  connection.getInputStream();
            JSONObject jsonObject = Get_data.getJsonObject(in);
            String Filestring = (String) jsonObject.get("file");
            /*File file = new File(path+"/" + name +"."+ (String) jsonObject.get("filetype"));*/
            String filename = path+"/" + name +"."+ (String) jsonObject.get("filetype");
            //Filestring =  Base64Utils.encodeBase64File(Filestring);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                Filestring =  Base64.getEncoder().encodeToString(Filestring.getBytes("utf-8"));
            }
            Base64Utils.decoderBase64File(Filestring,filename);
/*            OutputStream outputStream = new FileOutputStream(file);

            outputStream.write(Filestring.getBytes(StandardCharsets.UTF_8));*/
            return (String) jsonObject.get("filetype");
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
